$(function () {
    $("#loading").dialog({
        width:50,
        height:60,
        modal:true,
        closeOnEscape : false,
        resizable:false,
        draggable:false,
        autoOpen:false,
        position:{
            my:"left+100 top+60",
            at:"left top",
            of: "#login_box"
        }

    }).parent().find('.ui-widget-header').hide();


});


/**
 * id
 * title
 * my
 * at
 * of
 * content
 * @type {Function}
 */
function showMessage(id,title,my,at,of,content){
    $(id).dialog({
        title:title,
        autoOpen: true,
        draggable:false,
        position:{
            my:my,
            at:at,
            of:of
        }
    }).html(content);
}




function login(){
    $('#user-login-block').validate({
        submitHandler : function (form) {
            $(form).ajaxSubmit({
                url:LOGIN['LOGIN_URL'],
                type : 'POST',
                dataType:"json",
                beforeSubmit : function () {
                    $('#loading').dialog('open');
                },
                success : function (responseText) {
                    if(responseText){
                        if(responseText['status']=='ok'){
                            var html = '';
                            html += '<div class="status-grid-head">';
                            html += '<img id="tx-ct" src="'+LOGIN['IMG_PATH']+responseText['bg_url']+'" title="people-name"/>';
                            html += '</div>';
                            html += '<div class="status-people-pic">';
                            html += '<a href="'+LOGIN['AVATAR_URL']+'">';
                            html += responseText['avatar_img'];
                            html += '<span class="bg"></span><span class="text">添加/更换头像</span>';
                            html += '</a>';
                            html += '<img class="people-avilabel" src="'+LOGIN['IMG']+'/avila.png" title="avilabel"/>';
                            html += '</div>';
                            html += '<div id="tip-message"></div>';
                            html += '<div class="status-people-info">';
                            if(responseText['nick_name']==''){
                                html += '<h2><a href="'+LOGIN['USER_URL']+'">'+responseText['username']+'</a></h2>';
                            }else{
                                html += '<h2><a href="'+LOGIN['USER_URL']+'">'+responseText['nick_name']+'</a></h2>';
                            }
                            if(responseText['get_message']){
                                html += '<p class="ico"><a class="top-user-message" href="javascript:void(0);">消息(<i>'+responseText['get_message']+'</i>)</a></p>';
                            }else{
                                html += '<p class="ico"><a class="top-user-message" href="javascript:void(0);">消息</a></p>';
                            }

                            if(responseText['nick_name'] !=null){
                                html += '<p class="status-people-content">'+responseText['feeling']+'</p>';
                            }
                            html += '</div>';
                            html += '<div class="status-corrent">';
                            html += '<ul>';
                            html += '<li><a class="appont" href="'+LOGIN['USER_URL']+'">用户中心</a></li>';
                            html += '<li><a class="client" href="'+LOGIN['HOME_URL']+responseText['domain']+'">我的主页</a></li>';
                            html += '<li><a class="report" href="javascript:logout();">退出</a></li>';
                            html += '</ul>';
                            html += '<div class="clear"></div>';
                            html += '</div>';
                            html += '<div class="clear"></div>';
                            setTimeout(function () {
                                $('#loading').dialog('close');
                                $("#login_box_2").html(html).addClass("in").removeClass("out");
                                $("#login_box_1").addClass("out").removeClass("in");
                                $("#login_box_1").html('');
//                                if(responseText['info_status'] == '0'){
//                                    showMessage('#tip-message','系统提示',"left+20 top-75","left top","#login_box",'你还有没有完成的任务哦！请点<a class="qtip-blue" href="#">这里</a><br/>完成任务可获得<span class="qtip-red">10积分</span>');
//                                    showMessage('#tip-message','系统提示',"left+20 top-75","left top","#login_box",'你还有没有完成的任务哦！请点<a class="qtip-blue" href="#">这里</a><br/>');
//                                }

                            }, 1000);
                            window.location.reload();
                        }else if(responseText['status'] == 'error'){
                            $('#loading').dialog('close');
                            var messagesList = $('#get--messages .messages__list');
                            messagesList.html('<li><label class="error" style="display: block;">'+responseText['message']+'</label></li>')
                            messagesList.show();
                            setTimeout(function () {
                                messagesList.hide();
                                messagesList.html('');
                            }, 2000);
                        }
                    }
                }
            });
        },
        errorLabelContainer : 'ul.messages__list',
        wrapper : 'li',
        showErrors : function (errorMap, errorList) {
            this.defaultShowErrors();
        },
        highlight : function (element, errorClass) {
            $("ul.messages__list").show();
            $(element).css('border', '1px solid rgba(230, 0, 0, 0.8)');
            $(element).css('box-shadow','0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px rgba(230, 0, 0, 0.6)');
        },
        unhighlight : function (element, errorClass) {
            $(element).css('border', '1px solid rgba(0, 100, 0, 0.8)');
            $(element).css('box-shadow','0 1px 1px rgba(0, 0, 0, 0.075) inset, 0 0 8px rgba(0, 100, 0, 0.6)');
        },
        rules:{
            username : {
                required : true,
                minlength : 2,
                maxlength : 20,
            },
            password : {
                required : true,
                minlength : 6,
                maxlength : 20,
            }
        },
        messages : {
            username : {
                required : '用户名必须填写',
                minlength : $.format('用户名必须大于{0}位'),
                maxlength : $.format('用户名必须小于{0}位！')
            },
            password : {
                required : '密码必须填写',
                minlength : $.format('密码必须大于{0}位'),
                maxlength : $.format('密码必须小于{0}位')
            }
        }
    });
}

function logout(){
    var url = LOGIN['LOGOUT_URL'];
    var logout_status = LOGIN['LOGIN_OUT'];
    var home_url = LOGIN['HOME_URL'];
    $.ajax({
        url:url,
        type:"get",
        beforeSend : function () {
            $('#loading').dialog('open');
        },
        success:function(data){
            if(data=='ok'){
                if(logout_status ==1){
                    setTimeout(function () {
                        $('#loading').dialog('close');
                        window.location.href = home_url;
                    }, 1000);
                }else{
                    var html = '';
                    html += '<div class="login">';
                    html += '<form id="user-login-block">';
                    html += '<div class="form-item form-type-textfield form-item-name">';
                    html += '<input type="text" class="form-text" maxlength="60" size="15" value="" name="username" id="username" onfocus="if (this.value == \'Username\') {this.value = \'\';}" onblur="if (this.value == \'\') {this.value = \'\';}" placeholder="用户名或者邮箱">';
                    html += '</div>';
                    html += '<div class="form-item form-type-password form-item-pass">';
                    html += '<input type="password" class="form-text" maxlength="60" size="15" name="password" id="password" placeholder="请输入密码">';
                    html += '</div>';
                    html += '<div class="form-item">';
                    html += '<label class="auto" for="auto"><input checked="checked" type="checkbox" name="auto" id="auto"/>下一次自动登录</label>';
                    html += '<a href="'+LOGIN['FORGET_URL']+'" class="forget_password">忘记密码</a>';
                    html += '</div>';
                    html += '<div id="edit-actions--2" class="form-actions form-wrapper">';
                    html += '<input type="submit" class="form-submit" onclick="login();" value="登   录" name="op" id="edit-submit--2">';
                    html += '</div>';
                    html += '</form>';
                    html += '<div id="get--messages">';
                    html += '<div class="messages--error">';
                    html += '<ul class="messages__list" style="display: none;">';
                    html += '</ul>';
                    html += '</div>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="status-corrent">';
                    html += '<ul>';
                    html += '<li><a class="appont"  href="'+LOGIN['REG_URL']+'">注册</a></li>';
                    html += '<li><a class="client qq_login" href="'+LOGIN['QQ_URL']+'">QQ登录</a></li>';
                    html += '<li><a class="report weibo_login" href="'+LOGIN['SINA_URL']+'">微博登录</a></li>';
                    html += '<div class="clear"></div>';
                    html += '</ul>';
                    html += '</div>';
                    html += '<div class="clear"></div>';
//                $('#tip-message').html('').dialog('close');
                    setTimeout(function () {
                        $('#loading').dialog('close');
                        $("#login_box_1").html(html).addClass("in").removeClass("out");
                        $("#login_box_2").addClass("out").removeClass("in");
                        $("#login_box_2").html('');
                    }, 1000);
                    window.location.reload();
                }
            }
        }

    });

}